/**
 * This file is part of the FlexiTimer project.
 * 
 * Author: Florian Arndt <f.aarndt@web.de>
 *  	   Clemens Teichmann <>
 *
 * http://code.google.com/p/flexitimer/ 
 */

package flexitimer_client.calc;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.Timer;

import flexitimer_client.gui.frame;
import flexitimer_client.gui.optionFrame;

public class autoUpdateAction implements ActionListener {

	public 	Timer 	timer = null;
	private static boolean isCountdownStarted = false;
	private static autoUpdateAction instance = null;
	
	private autoUpdateAction() {
		
	}
	
	@SuppressWarnings("deprecation")
	@Override
	public void actionPerformed(ActionEvent e) {
		int 	delay = 1000;	

		ActionListener updatePerformer = new ActionListener() {
			@SuppressWarnings("null")
			public void actionPerformed(ActionEvent evt) {
				SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
				Date startTime = null;
				Date workTime = null;
				Date endTime = null;
				int difHour1;
				int difMin1;
				int difHour2;
				int difMin2;
				Date maxTime = null;
				Date aktuell = new Date();
				int interval = optionFrame.getInstance().getPopupInterval();
				
				try {
					startTime = sdf.parse(frame.getInstance().getStartTime());
					endTime = sdf.parse(frame.getInstance().getEndTime());
					maxTime = sdf.parse(optionFrame.getInstance().getMaxTime());
					workTime = sdf.parse("00:00");
				} catch (ParseException e1) {
					//frame.getInstance().addText("ERROR: Zeit falsch eingegeben! (hh:mm)");
				}
				
				workTime.setHours(startTime.getHours() + maxTime.getHours());
				workTime.setMinutes(startTime.getMinutes()
						+ maxTime.getMinutes());
				difHour1 = workTime.getHours() - aktuell.getHours();
				difMin1 = workTime.getMinutes() - aktuell.getMinutes();
				difHour2 = endTime.getHours() - aktuell.getHours();
				difMin2 = endTime.getMinutes() - aktuell.getMinutes();
				if (difMin1 < 0) {
					difHour1 -= 1;
					difMin1 += 60;
				}
				if (difMin2 < 0) {
					difHour2 -= 1;
					difMin2 += 60;
				}
				if (difHour2 < 0) {
					difHour2 = 0;
					difMin2 = 0;
				}
				if (difHour1 < 0) {
					difHour1 = 0;
					difMin1 = 0;
				}
				frame.getInstance().addProgress(
						" Countdown(max): " + difHour1 + ":"
								+ String.format("%02d", difMin1) + 
								" Countdown(Endzeit): " +
								difHour2 + ":"
								+ String.format("%02d", difMin2));
				frame.getInstance().setFrameTitle(
						"Noch " +
								difHour2 + ":"
								+ String.format("%02d", difMin2));
                if(interval!=0) {
					int difMinPop = difMin2 + difHour2 * 60;

					if (difMinPop % interval == 0 && timer.getDelay() == 1000) {
						frame.getInstance().trayPopupMessage(
								"TIMER",
								" Countdown(max): \t" + difHour1 + ":"
										+ String.format("%02d", difMin1)
										+ "\nCountdown(Endzeit): \t" + difHour2
										+ ":" + String.format("%02d", difMin2));
						timer.setDelay(1001);
					}
					if (difMinPop % interval != 0 && timer.getDelay() == 1001) {
						timer.setDelay(1000);
					}
				} else {
					if (difMin2 == 0 && timer.getDelay() == 1000) {
						frame.getInstance().trayPopupMessage(
								"TIMER",
								" Countdown(max): \t" + difHour1 + ":"
										+ String.format("%02d", difMin1)
										+ "\nCountdown(Endzeit): \t" +
									difHour2 + ":"
									+ String.format("%02d", difMin2));
						timer.setDelay(1001);
					}
					if(difMin2 != 0 && timer.getDelay() == 1001){timer.setDelay(1000);}
				}
			}			
		};
		if(!isCountdownStarted) {
			isCountdownStarted = true;
			frame.getInstance().setCountdownButton("Countdown stoppen");
			frame.getInstance().setCountdownPopupButton("Stop Countdown");
			timer = new Timer(delay, updatePerformer);
			timer.start();
		}
		else {
			frame.getInstance().setCountdownButton("Countdown starten");
			frame.getInstance().setCountdownPopupButton("Start Countdown");
			timer.stop();
			frame.getInstance().setFrameTitle("Gleitzeit Berechnung");
			frame.getInstance().addProgress("");
			isCountdownStarted = false;
		}
	}
	
	public static boolean getCountdownStatus() {
		return isCountdownStarted;
	}
	
	public static autoUpdateAction getInstance() {
		if(instance == null)
			instance = new autoUpdateAction();
		return instance;
	}
}
